<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客注册页</title>

    <!-- 复用登录页的样式文件，保证布局一致 -->
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>

<body>
<!-- 导航栏，和登录页完全一样 -->
<div class="nav">
    <img src="pic/nuc_logo.jpg" alt="">
    <span class="blog-title">网页聊天系统</span>
    <div class="space"></div>
</div>

<!-- 注册容器，结构和登录页对应容器一致 -->
<div class="container-login">
    <div class="login-dialog">
        <h3>注册</h3>
        <div class="row">
            <span>用户名</span>
            <input type="text" name="username" id="regUsername">
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" name="password" id="regPassword">
        </div>
        <div class="row">
            <button id="submit" onclick="register()">提交</button>
        </div>

        <div class="row">
            <span>已有账号？</span>
            <a href="chatroom_login.html" class="login-link">点击登录</a>
        </div>

    </div>
</div>

<!-- 引入jQuery，和登录页一致 -->
<script src="js/jquery.min.js"></script>
<script>
    function register() {
        // 获取表单值，调试用
        console.log("用户名: " + $("#regUsername").val());
        console.log("密码: " + $("#regPassword").val());

        // 表单验证
        if (!$("#regUsername").val() || !$("#regPassword").val()) {
            alert("用户名和密码为必填项");
            return;
        }

        $.ajax({
            type: "post",
            url: "/user/register",
            contentType: "application/json",
            data: JSON.stringify({
                username: $("#regUsername").val(),
                password: $("#regPassword").val(),
            }),
            success: function (result) {
                console.log("注册响应: ", result);
                if (result != null && result.code == "SUCCESS") {
                    alert("注册成功，请登录");
                    location.href = "chatroom_login.html";
                } else {
                    alert(result.errMsg || "注册失败，请重试");
                }
            },
            error: function (xhr, status, error) {
                console.error("注册请求错误: ", error);
                alert("注册请求失败，请稍后再试");
            }
        });
    }

    // 也可以像登录页一样，用 jQuery 事件绑定方式（二选一即可，下面是可选的另一种写法示例）
    // $(document).ready(function () {
    //     $("#registerBtn").click(function () {
    //         register();
    //     });
    // });
</script>
</body>

</html>